package com.example.administrator.testxingpiao.ui.main;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.administrator.testxingpiao.R;
import com.example.administrator.testxingpiao.base.BaseActivity;
import com.example.administrator.testxingpiao.ui.main.exercise.ExerciseFragment;
import com.example.administrator.testxingpiao.ui.main.homepage.HomePageFragment;
import com.example.administrator.testxingpiao.ui.main.mine.MineFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class MainActivity extends BaseActivity {

    public static final int SELECTED_HOMEPAGE = 0;
    public static final int SELECTED_ACTIVITY= 1;
    public static final int SELECTED_MINE = 2;

    @BindView(R.id.fl_container_main)
    FrameLayout flContainerMain;
    @BindView(R.id.bottom_imageview_homepage_main)
    ImageView bottomImageviewHomepageMain;
    @BindView(R.id.bottom_textview_homepage_main)
    TextView bottomTextviewHomepageMain;
    @BindView(R.id.rl_homepage)
    RelativeLayout rlHomepage;
    @BindView(R.id.bottom_imageview_activity_main)
    ImageView bottomImageviewActivityMain;
    @BindView(R.id.bottom_textview_acitivty_main)
    TextView bottomTextviewAcitivtyMain;
    @BindView(R.id.rl_activity)
    RelativeLayout rlActivity;
    @BindView(R.id.bottom_imageview_mine_main)
    ImageView bottomImageviewMineMain;
    @BindView(R.id.bottom_textview_mine_main)
    TextView bottomTextviewMineMain;
    @BindView(R.id.rl_mine)
    RelativeLayout rlMine;

    private HomePageFragment homePageFragment;
    private ExerciseFragment activityFragment;
    private MineFragment mineFragment;
    private FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initFramgnet();

    }

    private void initFramgnet() {

        selectButtonSelected(SELECTED_HOMEPAGE);
        fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        homePageFragment = new HomePageFragment();
        activityFragment = new ExerciseFragment();
        mineFragment = new MineFragment();
        transaction.add(R.id.fl_container_main,homePageFragment);
        transaction.add(R.id.fl_container_main,activityFragment);
        transaction.add(R.id.fl_container_main,mineFragment);
        transaction.commit();

        selectFragment(SELECTED_HOMEPAGE);
    }


    @OnClick({R.id.rl_homepage, R.id.rl_activity, R.id.rl_mine})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_homepage:
                selectFragment(SELECTED_HOMEPAGE);
                break;
            case R.id.rl_activity:
                selectFragment(SELECTED_ACTIVITY);
                break;
            case R.id.rl_mine:
                selectFragment(SELECTED_MINE);
                break;
        }
    }

    private void selectFragment(int position) {
        setBottonSelectedFalse();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.hide(homePageFragment).hide(activityFragment).hide(mineFragment);
        switch (position) {
            case SELECTED_HOMEPAGE:
                transaction.show(homePageFragment);
                selectButtonSelected(SELECTED_HOMEPAGE);
                break;
            case SELECTED_ACTIVITY:
                transaction.show(activityFragment);
                selectButtonSelected(SELECTED_ACTIVITY);
                break;
            case SELECTED_MINE:
                transaction.show(mineFragment);
                selectButtonSelected(SELECTED_MINE);
                break;
        }
        transaction.commit();
    }

    private void setBottonSelectedFalse() {

        bottomImageviewHomepageMain.setSelected(false);
        bottomTextviewHomepageMain.setSelected(false);
        bottomImageviewActivityMain.setSelected(false);
        bottomTextviewAcitivtyMain.setSelected(false);
        bottomImageviewMineMain.setSelected(false);
        bottomTextviewMineMain.setSelected(false);
    }

    private void selectButtonSelected(int position){
        switch (position) {
            case SELECTED_HOMEPAGE:
                bottomImageviewHomepageMain.setSelected(true);
                bottomTextviewHomepageMain.setSelected(true);
                break;
            case SELECTED_ACTIVITY:
                bottomImageviewActivityMain.setSelected(true);
                bottomTextviewAcitivtyMain.setSelected(true);
                break;
            case SELECTED_MINE:
                bottomImageviewMineMain.setSelected(true);
                bottomTextviewMineMain.setSelected(true);
                break;
        }
    }
}
